<?php


namespace app\admin\member\model;

use Exception;
use mb\helper\Collection;
use think\Db;

class Message
{

    protected static $typeTitle = [
        'addMoney' => '购单提示',
        'pushMoney' => '回款提示',
         'caijin' => '彩金赠送',
    ];
    protected static $table = 'member_messages';

    /**
     * @param array $row
     * @return bool|int|string
     */
    public static function add(array $row)
    {
        $newRow = Collection::elements([
            'memberId',
            'content',
            'type',
            'targetId',
            'money',
        ], $row);
        $newRow['timeCreated'] = time();
        $newRow['affirm'] = 0;
        $newRow['existence'] = 1;
        $newRow = Collection::keyStyle($newRow, Collection::NAME_STYLE_C);
        try {
            return Db::table(self::$table)
                ->insertGetId($newRow);
        } catch (Exception $e) {
            return error($e->getCode(), $e->getMessage());
        }
    }

    /**
     * @param $filter
     * @return array|Error
     */
    public static function fetch($filter)
    {
        $where = self::parseFilters($filter);
        try {
            $row = Db::table(self::$table)
                ->where($where)
                ->find();
            if (!empty($row)) {
                $row['time_created'] *= 1000;
                return Collection::keyStyle($row, Collection::NAME_STYLE_JAVA);
            }
        } catch (Exception $e) {
            return error($e->getCode(), $e->getMessage());
        }
        return [];
    }

    /**
     * @param $filter
     * @return bool|Error
     */
    public static function remove($filter)
    {
        $where = self::parseFilters($filter);
        try {
            $offect = Db::table(self::$table)
                ->where($where)
                ->delete();
            if ($offect === 1) {
                return true;
            }
        } catch (Exception $e) {
            return error($e->getCode(), $e->getMessage());
        }
        return $offect;
    }

    /**
     * @param $filter
     * @param $newState
     * @return bool|Error
     */
    public static function update($filter, $newState)
    {
        $where = self::parseFilters($filter);
        try {
            $offect = Db::table(self::$table)
                ->where($where)
                ->update($newState);
            if ($offect === 1) {
                return true;
            }
        } catch (Exception $e) {
            return error($e->getCode(), $e->getMessage());
        }
        return $offect;
    }

    /**
     * @param $filter
     * @return array
     */
    public static function parseFilters($filter)
    {
        $where = [];
        if (is_array($filter)) {
            if (!empty($filter['id'])) {
                $where[] = ['id', '=', $filter['id']];
            }
            if (!empty($filter['memberId'])) {
                $where[] = ['member_id', '=', $filter['memberId']];
            }
            if (empty($where)) {
                throw error(-10, '参数错误必须指定id');
            }
            if (isset($filter['affirm'])) {
                $where[] = ['affirm', '=', $filter['affirm']];
            }
        } else {
            $where[] = ['id', '=', intval($filter)];
        }
        return $where;
    }

    /**
     * @param array $filters
     * @param int $pIndex
     * @param int $pSize
     * @param int $total
     * @return array|Error
     */
    public static function search(array $filters, int $pIndex = 0, int $pSize = 10, int &$total = 0)
    {
        $where = [];
//        if (!empty($filters['timeCreatedStart'])) {
//            $where[] = ['time_created', '=>', $filters['timeCreatedStart']];
//        }
//        if (!empty($filters['timeCreatedEnd'])) {
//            $where[] = ['time_created', '<=', $filters['timeCreatedEnd']];
//        }
        if (!empty($filters['type'])) {
            $where[] = ['type', '=', $filters['type']];
        }
        if (isset($filters['affirm'])) {
            $where[] = ['affirm', '=', $filters['affirm']];
        }
        if (!empty($filters['memberId'])) {
            $where[] = ['member_id', '=', $filters['memberId']];
        }
        if (!empty($filters['existence'])) {
            $where[] = ['existence', '=', $filters['existence']];
        }
        try {
            if (!empty($pIndex)) {
                $total = Db::table(self::$table)
                    ->where($where)
                    ->count();
            }
            $query = Db::table(self::$table)
                ->where($where);
            if (!empty($pIndex)) {
                $query->page($pIndex, $pSize);
            }
            if (!empty($filters['order'])) {
                $query->order($filters['order']);
            }
            $dataSet = $query->select();
            if (!$dataSet->isEmpty()) {
                return $dataSet->map(
                    function ($row) {
                        $row['typeTitle'] = self::$typeTitle[$row['type']];
                        $row['time_created'] *= 1000;
                        $row = Collection::keyStyle($row, Collection::NAME_STYLE_JAVA);
                        return $row;
                    }
                )->toArray();
            }
        } catch (Exception $e) {
            return error($e->getCode(), $e->getMessage());
        }
        return [];
    }

    public static function handlerAdd($row)
    {
        switch ($row['type']) {
            case 'addMoney':
                $row['content'] = '用户购单金额:' . $row['value'];
                $row['money'] = $row['value'];
                break;
            case 'pushMoney':
                $row['content'] = '系统转出金额:' . $row['value'] * -1;
                $row['money'] = $row['value'] * -1;
                break;
             case 'caijin':
                $row['content'] = '系统赠送彩金:' . $row['value'];
                $row['money'] = $row['value'];
                break;
            default:
                return error(-10, '错误的type类型');
        }

        return self::add($row);
    }

    public static function countNoReady($filters)
    {
        $where = self::parseFilters($filters);
        try {
            return Db::table(self::$table)->where($where)->count();
        } catch (Exception $e) {
            return error($e->getCode(), $e->getMessage());
        }

    }
}